require(['./config'], () => {
  require(['jquery', 'popper', 'header', 'bootstrap'], () => {
    class Login {
      constructor() {
        this.load()
        this.lo()
      }
      load() {
        console.log($)
      }
      lo() {
        //获取账号和 密码的值
        const loname = $('#lo_username'),
          lopwd = $('#lo_userpwd'),
          btn = $('#register_btn')
        let a = [JSON.parse(localStorage.getItem('usernameList'))]
        btn.on('click', function () {
          if (loname.val() === '' || lopwd.val() === '') {
            alert('请输入正确密码!')
          } else {
            console.log('可以登录')
            if (a[0]) {
              let ss = []
              a.map(item => {
                item.map(item1 => {
                  if (item1.username === loname.val()) {
                    console.log('success');
                    ss.push(item1)
                    return ss
                  }
                })
              })
              if (ss[0]) {
                const flag = ss.some(item => {
                  return item.username === loname.val() && item.pwd === lopwd.val()
                })
                console.log(flag)
                if (flag) {
                  //true
                  //写入cookie cookie只存登录状态
                  function setCookie(key, value, options = {
                    expires: new Date(),
                    path: '/'
                  }) {
                    document.cookie = `${key}=${encodeURIComponent(value)};expires=${options.expires};path=${options.path}`
                  }
                  setCookie('username', loname.val(), {
                    path: '/',
                    expires: new Date('2020-12-20 00:00:00')
                  })
                  alert('登录成功！')
                  // window.location.href = `/index.html?username=${loname.val()}`
                  window.location.href = `/index.html`
                } else {
                  alert('账号或者密码错误！')
                  loname.val('')
                  lopwd.val('')
                }
              } else {
                alert('请先注册！')
                window.location.href = '/html/register.html'
              }
            } else {
              alert('先去注册！')
              window.location.href = '/html/register.html'
            }

          }
        })
      }
    }
    return new Login()
  })
})